package com.nhh.v.controller;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.nhh.v.anotation.ValidateHeader;
import com.nhh.v.dto.RequiredRequestHeader;
import com.nhh.v.service.HashRateService;
import com.nhh.v.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ywp on 2018/9/6.
 */
@RestController
@RequestMapping("/api/hashrate")
public class HashRateController {

    @Autowired
    private HashRateService hashrateService;
    @Autowired
    private UsersService usersService;

    @PostMapping(value = "qrHashrateFlow")
    public Mono<Map> qrHashRateFlow(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "psize", required = false, defaultValue = "10") String pSize,
            @RequestParam(value = "pindex", required = false, defaultValue = "0") String pIndex) {

        HashMap<String, Object> hashMap = Maps.newHashMap();
        if ("0".equals(pIndex)) {
            hashMap.put("hashrateInfo", usersService.getHashRateInfo(Integer.valueOf(requireHeader.getUid())));
        }
        PageHelper.startPage(Integer.valueOf(pIndex), Integer.valueOf(pSize));
        hashMap.put("rows", hashrateService.getUserHashRateList(Integer.valueOf(requireHeader.getUid())));
        hashMap.put("psize", pSize);

        return Mono.just(hashMap);
    }
}
